package com.eda;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import com.eda.common.DataBean;
import com.eda.common.MyListView;
import com.eda.common.MyListView.OnRefreshListener;

public class PubMycargoActivity extends Activity {

	LayoutInflater inflater;
	SimpleAdapter adapter;
	MyListView listView;
	List<Map<String, String>> list;
	String out;
	DataBean bean ;
	String userId ;
	SharedPreferences loginInfo;
	TextView text;
	int total;
	LinearLayout listLayout;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_pub_mycargo_tab);

		bean = new DataBean();
		loginInfo = getSharedPreferences("LOGGING_INFO", 0);
		Button search = (Button) this.getParent().findViewById(
				R.id.commonFunction);
		search.setOnClickListener(searchMyTruckListener);

		/* 搜索 我的货源 */
		searchDataList(0,10,1);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.pub_mytruck_tab, menu);
		return true;
	}

	OnClickListener searchMyTruckListener = new OnClickListener() {

		@Override
		public void onClick(View arg0) {
			Intent main2PubSearchIntent = new Intent(PubMycargoActivity.this,
					OwnerActivity.class);
			main2PubSearchIntent.putExtra("targetTab", 0);
			startActivityForResult(main2PubSearchIntent,-1);
		}
	};

	private void searchDataList(int from, int to,int type) {
		List<Map<String, String>> allresult = generateDataList(from,to);
		total = Integer.parseInt(allresult.get(allresult.size()-1).get("total"));
		setContent(total,allresult,type);
	}
	
	private void setContent(int total,List<Map<String, String>> allresult,int type){
		allresult.remove(allresult.size()-1);
		List<Map<String, String>> tempList =  allresult;
		if(type==1){
			list = tempList;
		}else{
			for(int i=0;i<tempList.size()-1;i++){
				list.add(i,tempList.get(tempList.size()-1-i));
			}
		}
		
		if(type==1){
			adapter = new SimpleAdapter(this, list, R.layout.listview_content,
					new String[] { "title", "info1", "info2"}, new int[] {
							R.id.title, R.id.info1, R.id.info2});
			listView = new MyListView(this);
			listView.setAdapter(adapter);

			listView.setonRefreshListener(refresh);

			inflater = LayoutInflater.from(this);
			listLayout = (LinearLayout) inflater.inflate(
					R.layout.truck_list, null);
			text = (TextView)listLayout.findViewById(R.id.listtotal);
			text.setText("共找到"+total+"条货源");
			listLayout.addView(listView);
			setContentView(listLayout);
		}
	}

	OnRefreshListener refresh = new OnRefreshListener() {

		@Override
		public void onRefresh() {
			new AsyncTask<Void, Void, Void>() {
				protected Void doInBackground(Void... params) {
					try {
						Thread.sleep(1000);
					} catch (Exception e) {
						e.printStackTrace();
					}
					if(total>=list.size()){
						searchDataList(list.size(),list.size()+10,2);
					}
					return null;
				}

				@Override
				protected void onPostExecute(Void result) {
					adapter.notifyDataSetChanged();
					listView.onRefreshComplete();
				}

			}.execute(null);
		}
	};
	
	private List<Map<String, String>> generateDataList(int from ,int to) {
		userId = loginInfo.getString("USER_NAME", "");
		Map<String,String> params = new HashMap<String,String>();
		params.put("userId", userId);
		params.put("from", String.valueOf(from));
		params.put("to", String.valueOf(to));
		return bean.getMyCargo(params);
	}
}
